import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;

public class LangConvert {

	private static ArrayList extList = new ArrayList();
	private static ArrayList noList = new ArrayList();
	private static ConvertWriter convertedLog;

	{
		// 체크할 확장자 
		Collections.addAll(extList, CONSTANTS.allowExtList);

		// 체크하지 않을 파일들
		Collections.addAll(noList, CONSTANTS.notAllowFileList);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		LangConvert convert = new LangConvert();
		
		if(args.length != 1) {
			System.out.println("Usage : java -jar LangConvert.jar d:/workspace/nenote");
			System.exit(0);
		}
		
		File source = new File(args[0]);
		File target = new File(source.getAbsoluteFile() + "_converted");
		
		convertedLog = new ConvertWriter(target.getAbsoluteFile()+ System.getProperty("file.separator") + "converted_" + new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime()) + ".log");

		// 한글 체크할 디렉토리를 output 디렉토리로 복사 후 작업
		try {

			if (target.exists()) {
				System.err.println("디렉토리 삭제");
				DirHelper.deleteDir(target);
			}

			System.err.println("디렉토리 복사 시작");
			DirHelper.copyDirectory(source, target);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.err.println();
		convert.Read(target);
	}

	private void Read(File readFile) {
		// TODO Auto-generated method stub

		File targetDir = readFile;

		String[] targetDirList = targetDir.list();

		int targetDirListCnt = targetDirList.length;

		String fileName = null;
		for (int i = 0; i < targetDirListCnt; i++) {
			File file = new File(readFile.getAbsolutePath()
					+ System.getProperty("file.separator") + targetDirList[i]);
			fileName = file.getName();

			if (fileName.substring(0, 1) == ".")
				continue;

			if (file.isDirectory()) {
				Read(file);
			} else {
				int splitPoint = fileName.lastIndexOf(".");
				if (splitPoint > 0) {
					String ext = fileName.substring(splitPoint + 1)
							.toLowerCase();
					if (!ext.isEmpty() && extList.contains(ext)
							&& !noList.contains(fileName)) {
						FileConvert fc = new FileConvert(file, convertedLog);
					}
				}
			}
		}
	}

}
